local skel = fk.CreateSkill {
  name = "emo__yumos",
}

Fk:loadTranslationTable{
  ["emo__yumos"] = "驭魔",
  [":emo__yumos"] = "你可以弃置一张牌并移去一张“魔物”，视为使用一张【树上开花】或与此“魔物”同名的牌。若弃置的牌与此“魔物”：花色相同，以此法使用的牌可额外指定一个目标；类型相同，你摸一张牌。",

  ["#emo__yumos"] = "驭魔:弃置一张牌，选择一种牌名，移去一张“魔物”视为使用之",
  ["#emo__yumos-remove"] = "驭魔：请移去一张“魔物”",
  ["#emo__yumos-target"] = "驭魔：可以为 %arg 额外指定一个目标",
}

skel:addEffect("viewas", {
  pattern = ".|.|.|.|.|trick,basic",
  interaction = function (self, player)
    local names = {}
    if Fk.currentResponsePattern == nil then table.insert(names, "bogus_flower") end
    for _, id in ipairs(player:getPile("yinwu_monster")) do
      local card = Fk:cloneCard(Fk:getCardById(id).name)
      if ((Fk.currentResponsePattern == nil and player:canUse(card)) or
        (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(card))) then
        table.insertIfNeed(names, card.name)
      end
    end
    if #names == 0 then return end
    return UI.CardNameBox {choices = names}
  end,
  view_as = function (self, player, cards)
    if #cards ~= 1 or not self.interaction.data then return nil end
    local c = Fk:cloneCard(self.interaction.data)
    c.skillName = skel.name
    c:setMark("emo__yumos_throw", cards[1])
    return c
  end,
  card_filter = function (self, player, to_select, selected)
    return #selected == 0 and not player:prohibitDiscard(Fk:getCardById(to_select))
  end,
  before_use = function(self, player, use)
    local room = player.room
    local to_throw = use.card:getMark("emo__yumos_throw")
    if to_throw == 0 then return end
    room:setCardMark(use.card, "emo__yumos_throw", 0)
    local cards = player:getPile("yinwu_monster")
    if use.card.name ~= "bogus_flower" then
      cards = table.filter(cards, function(id) return Fk:getCardById(id).name == use.card.name end)
    end
    local to_use
    if #cards == 0 then
      return ""
    elseif #cards == 1 then
      to_use = cards[1]
    else
      to_use = room:askToChooseCard(player, {
        target = player, skill_name = skel.name, prompt = "#emo__yumos-remove",
        flag = { card_data = { { "yinwu_monster", cards } } },
      })
    end
    room:moveCards({
      from = player,
      ids = {to_throw},
      toArea = Card.DiscardPile,
      moveReason = fk.ReasonDiscard,
      proposer = player,
      skillName = skel.name,
    },{
      from = player,
      ids = {to_use},
      --moveInfo = {{cardId = to_use, fromArea = Card.PlayerSpecial, fromSpecialName = "yinwu_monster" }},
      toArea = Card.DiscardPile,
      moveReason = fk.ReasonPutIntoDiscardPile,
      proposer = player,
      skillName = skel.name,
    })
    if player.dead then return end
    use.extra_data = use.extra_data or {}
    use.extra_data.emo__yumos_user = player.id
    if Fk:getCardById(to_throw).suit == Fk:getCardById(to_use).suit then
      use.extra_data.emo__yumos_extraTar = true
    end
    if Fk:getCardById(to_throw).type == Fk:getCardById(to_use).type then
      use.extra_data.emo__yumos_draw = true
    end
  end,
  enabled_at_play = function(self, player)
    if not player:isNude() and #player:getPile("yinwu_monster") > 0 then
      local flower = Fk:cloneCard("bogus_flower")
      if player:canUse(flower) and not player:prohibitUse(flower) then return true end
      for _, id in ipairs(player:getPile("yinwu_monster")) do
        local card = Fk:cloneCard(Fk:getCardById(id).name)
        if player:canUse(card) and not player:prohibitUse(card) then
          return true
        end
      end
    end
  end,
  enabled_at_response = function (self, player, response)
    if not player:isNude() and #player:getPile("yinwu_monster") > 0 and not response and Fk.currentResponsePattern then
      for _, id in ipairs(player:getPile("yinwu_monster")) do
        local card = Fk:cloneCard(Fk:getCardById(id).name)
        if Exppattern:Parse(Fk.currentResponsePattern):match(card) then
          return true
        end
      end
    end
  end,
})

skel:addEffect(fk.AfterCardTargetDeclared, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return not player.dead and data.extra_data and data.extra_data.emo__yumos_user == player.id
    and data.extra_data.emo__yumos_extraTar
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = data:getExtraTargets()
    if #targets > 0 then
      local tos = player.room:askToChoosePlayers(player, {
        targets = targets, min_num = 1, max_num = 1, skill_name = skel.name, cancelable = true,
        prompt = "#emo__yumos-target:::"..data.card.name,
      })
      if #tos > 0 then
        for _, to in ipairs(tos) do
          data:addTarget(to)
        end
        room:sendLog{
          type = "#AddTargetsBySkill",
          from = player.id,
          to = table.map(tos, Util.IdMapper),
          arg = skel.name,
          arg2 = data.card:toLogString()
        }
      end
    end
  end,
})

skel:addEffect(fk.CardUsing, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return not player.dead and data.extra_data and data.extra_data.emo__yumos_user == player.id
    and data.extra_data.emo__yumos_draw
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player:drawCards(1, skel.name)
  end,
})

return skel
